<include file="Public/header" title="寄件" />
<body onselectstart="return false">
<style>
    .form{
        float: left;
        width: 32%;
    }
    .danwei{
        position: absolute;
    }
</style>
<div class="main FromUpToDown" >
    <div class="content" style="position: relative">
        <div class="head-content">
            <div class="head-title">
                <span><{:ACTION_NAME == 'edit' ? '修改' : '添加'}>寄件</span>
            </div>
        </div>
        <form action='' id="form" >
            <div class="form-content">
                <div class="form">
                    <div class="form-left">寄件编号</div>
                    <div class="form-right">
                        <input class="form-input-small" type="text" name='no'  value="<{$info['no']}>" placeholder="寄件编号"  />
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">寄件重量</div>
                    <div class="form-right">
                        <input class="form-input-small" type="text" name='weight'  value="<{$info['weight']}>" placeholder="寄件重量"  /><span class="danwei">kg</span>
                    </div>
                </div>
                <div style="clear: both"></div>
                <div class="form">
                    <div class="form-left">价格模板</div>
                    <div class="form-right">
                        <select class="form-input-small" name="template_name" >
                            <option value="">选择价格模板</option>
                            <foreach name="template_name_list" item="vo">
                                <option value="<{$vo['template_name']}>"><{$vo['template_name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">寄件价格</div>
                    <div class="form-right" style="position:relative">
                        <input class="form-input-small" type="text" name='price'  value="<{$info['price']}>" placeholder="寄件价格"  /><span class="danwei">元</span>
                        <button style="position:absolute;left:200px" type='button' onclick="calculatePrice()"  class="form-button color-bg-green">计算价格</button>
                    </div>
                </div>

                <hr style="clear: both;" />
                <div class="form">
                    <div class="form-left">收件人姓名</div>
                    <div class="form-right">
                        <input class="form-input-small" type="text" name='accept_name'  value="<{$info['accept_name']}>" placeholder="收件人姓名"  />
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">收件人号码</div>
                    <div class="form-right">
                        <input class="form-input-small" type="text" name='accept_phone'  value="<{$info['accept_phone']}>" placeholder="收件人号码"  />
                    </div>
                </div>
                <div style="clear: both"></div>
                <div class="form">
                    <div class="form-left">收件人省</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="accept_province" id="accept_province">
                            <option value="">收件人省份</option>
                            <foreach name="province" item="vo" >
                                <option <{:$vo['id'] == $info['accept_province'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">收件人市</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="accept_city" id="accept_city">
                            <option value="">收件人市</option>
                            <foreach name="accept_city" item="vo" >
                                <option <{:$vo['id'] == $info['accept_city'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">收件人县</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="accept_county" id="accept_county">
                            <option value="">收件人县</option>
                            <foreach name="accept_county" item="vo" >
                                <option <{:$vo['id'] == $info['accept_county'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div style="clear: both"></div>
                <div class="form">
                    <div class="form-left">收件人镇(乡)</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="accept_town" id="accept_town">
                            <option value="">收件人乡(镇)</option>
                            <foreach name="accept_town" item="vo" >
                                <option <{:$vo['id'] == $info['accept_town'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form" >
                    <div class="form-left">收件人街道(村)</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="accept_village" id="accept_village">
                            <option value="">收件人街道(村)</option>
                            <foreach name="accept_village" item="vo" >
                                <option <{:$vo['id'] == $info['accept_village'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div style="clear: both"></div>
                <div class="form" style="width: 100%">
                    <div class="form-left">收件人详细地址</div>
                    <div class="form-right">
                        <input style="width: 500px"  class="form-input-small" type="text" name='accept_addr'  value="<{$info['accept_addr']}>" placeholder="收件人详细地址"  />
                    </div>
                </div>
                <hr style="clear: both;" />
                <div class="form">
                    <div class="form-left">寄件人姓名</div>
                    <div class="form-right">
                        <input class="form-input-small" type="text" name='sender_name'  value="<{$info['sender_name']}>" placeholder="寄件人姓名"  />
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">寄件人号码</div>
                    <div class="form-right">
                        <input class="form-input-small" type="text" name='sender_phone'  value="<{$info['sender_phone']}>" placeholder="寄件人号码"  />
                    </div>
                </div>
                <div style="clear: both"></div>
                <div class="form">
                    <div class="form-left">寄件人省</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="sender_province" id="sender_province">
                            <option value="">寄件人省</option>
                            <foreach name="province" item="vo" >
                                <option <{:$vo['id'] == $info['sender_province'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">寄件人市</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="sender_city" id="sender_city">
                            <option value="">寄件人市</option>
                            <foreach name="sender_city" item="vo" >
                                <option <{:$vo['id'] == $info['sender_city'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div class="form">
                    <div class="form-left">寄件人县</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="sender_county" id="sender_county">
                            <option value="">寄件人县</option>
                            <foreach name="sender_county" item="vo" >
                                <option <{:$vo['id'] == $info['sender_county'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>

                <div style="clear: both"></div>
                <div class="form">
                    <div class="form-left">寄件人街道</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="sender_town" id="sender_town">
                            <option value="">寄件人街道</option>
                            <foreach name="sender_town" item="vo" >
                                <option <{:$vo['id'] == $info['sender_town'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>

                <div class="form">
                    <div class="form-left">寄件人村</div>
                    <div class="form-right">
                        <select class="form-input-small"  name="sender_village" id="sender_village">
                            <foreach name="sender_village" item="vo" >
                                <option value="">寄件人村</option>
                                <option <{:$vo['id'] == $info['sender_village'] ? 'selected' : ''}> value="<{$vo['id']}>" ><{$vo['name']}></option>
                            </foreach>
                        </select>
                    </div>
                </div>
                <div style="clear: both"></div>
                <div class="form" style="width: 100%">
                    <div class="form-left">寄件人详细地址</div>
                    <div class="form-right">
                        <input style="width: 500px" class="form-input-small" type="text" name='sender_addr'  value="<{$info['sender_addr']}>" placeholder="寄件人详细地址"  />
                    </div>
                </div>
                <div style="clear: both"></div>
                <input type='hidden' name='id' value="<{$info['id']}>" >
                <div class="form-operate">
                    <button type='button' id="save"  class="form-button color-bg-green">保存</button>
                    <button type='button' onclick="history.go(-1)" class="form-button color-bg-grey">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
<include file="Public/footer"  />
<script>
    $("#save").click(function(){

        if($("input[name='no']").val() == ''){
            layer.tips('请输入寄件编号！', '#save', {tips: [2, 'red'], time: 1000});
            return;
        }
        if($("input[name='accept_name']").val() == ''){
            layer.tips('请输入收件人姓名！', '#save', {tips: [2, 'red'], time: 1000});
            return;
        }
        var reg = /1[34578]\d{9}/;
        if(!reg.test($("input[name='accept_phone']").val())){
            layer.tips('请输入正确手机号！', '#save', {tips: [2, 'red'], time: 1000});
            return;
        }
        $("#save").attr('disabled',true);
        $.post(
                "<{:U('save')}>",
                $("#form").serialize(),
                function(res){
                    if(res.status == 1){
                        layer.tips(res.info, '#save', {tips: [2, 'green'], time: 1000});
                        setTimeout("location.href = document.referrer",1000);
                    }else{
                        $("#save").attr('disabled',false);
                        layer.tips(res.info, '#save', {tips: [2, 'red'], time: 1000});
                    }
                }
        );
    });
</script>
<script>
    //收件人省市县联动
    $("#accept_province").change(function(){
        var accept_province = $(this).val();
        if(!accept_province) return;
        $.get(
                "<{:U('Address/ajaxChildren')}>",
                {id:accept_province},
                function(res){
                    if(res !== ''){
                        $("#accept_city").html(res);
                        $("#accept_county").html('');
                        $("#accept_town").html('');
                        $("#accept_village").html('');
                    }
                }
        );
    }
    );
    $("#accept_city").change(function(){
                var accept_city = $(this).val();
                if(!accept_city) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:accept_city},
                        function(res){
                            if(res !== ''){
                                $("#accept_county").html(res);
                                $("#accept_town").html('');
                            }
                        }
                );
            }
    );
    $("#accept_county").change(function(){
                var accept_county = $(this).val();
                if(!accept_county) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:accept_county},
                        function(res){
                            if(res !== ''){
                                $("#accept_town").html(res);
                            }
                        }
                );
            }
    );
    $("#accept_town").change(function(){
                var accept_town = $(this).val();
                if(!accept_town) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:accept_town},
                        function(res){
                            if(res !== ''){
                                $("#accept_village").html(res);
                            }
                        }
                );
            }
    );
</script>
<!--寄件人地址联动-->
<script>
    $("#sender_province").change(function(){
                var sender_province = $(this).val();
                if(!sender_province) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:sender_province},
                        function(res){
                            if(res !== ''){
                                $("#sender_city").html(res);
                                $("#sender_county").html('');
                                $("#sender_town").html('');
                                $("#sender_village").html('');
                            }
                        }
                );
            }
    );
    $("#sender_city").change(function(){
                var sender_city = $(this).val();
                if(!sender_city) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:sender_city},
                        function(res){
                            if(res !== ''){
                                $("#sender_county").html(res);
                                $("#sender_town").html('');
                            }
                        }
                );
            }
    );
    $("#sender_county").change(function(){
                var sender_county = $(this).val();
                if(!sender_county) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:sender_county},
                        function(res){
                            if(res !== ''){
                                $("#sender_town").html(res);
                            }
                        }
                );
            }
    );
    $("#sender_town").change(function(){
                var sender_town = $(this).val();
                if(!sender_town) return;
                $.get(
                        "<{:U('Address/ajaxChildren')}>",
                        {id:sender_town},
                        function(res){
                            if(res !== ''){
                                $("#sender_village").html(res);
                            }
                        }
                );
            }
    );
</script>
<script>
    //计算价格
    function calculatePrice()
    {
        var weight = $("input[name='weight']").val();
        var accept_province = $("#accept_province").val();
        var template_name = $("select[name='template_name']").val();
        if(!weight){
            layer.tips('请输入寄件重量！', 'input[name="weight"]', {tips: [2, 'red'], time: 1000});
            return;
        }
        if(!accept_province){
            layer.tips('请输入寄件省份！', '#accept_province', {tips: [2, 'red'], time: 1000});
            return;
        }
        if(!template_name){
            layer.tips('请选择价格模板！', "select[name='template_name']", {tips: [2, 'red'], time: 1000});
            return;
        }
        $.post(
                "<{:U('calculatePrice')}>",
                {
                    template_name:template_name,
                    weight:weight,
                    province_id:accept_province
                },function(res){
                    if(res.status == 1){
                        $("input[name='price']").val(res.info);
                    }else{
                        layer.tips(res.info, "input[name='price']", {tips: [2, 'red'], time: 1000});
                    }
                }
        );
    }
    $(function(){
        $("input[name='sender_phone']").bind("input propertychange",function(){
            var reg = /1[34578]\d{9}/;
            var phone = $(this).val();
            if(!reg.test(phone)){
                $("select[name='is_member']").val(1);
            }else{
                $.post(
                        "<{:U('member')}>",
                        {
                          phone:phone
                        },function(res){
                            if(res.status == 1){
                                $("select[name='is_member']").val(2);
                            }else{
                                $("select[name='is_member']").val(1);
                            }
                        }
                )
            }
        })
    })
	
	$("form input").keydown(function(e){		     
    if (e.which == 13) {// 判断所按是否回车键  
        var inputs = $("form").find(":text"); // 获取表单中的所有输入框  
        var idx = inputs.index(this); // 获取当前焦点输入框所处的位置  
        if (idx == inputs.length - 1) {// 判断是否是最后一个输入框               
            $("#save").click(); // 提交表单  
        } else {  
            inputs[idx + 1].focus(); // 设置焦点  
            inputs[idx + 1].select(); // 选中文字  
        }  
        return false;// 取消默认的提交行为  
    }  
	});

      $("input[name='accept_phone']").bind("input propertychange",function(){
            var reg = /^1[34578]\d{9}$/;
            var phone = $(this).val();
            if(reg.test($(this).val())){
                $.post(
                        "<{:U('Customer/getTemplateName')}>",
                        {
                            phone:phone
                        },function(res){
                           if(res.status == 1){
                                $("select[name='template_name']").val(res.info);
                           }
                        }
                );
            }
    })
</script>
</body>
</html>